import {
  providerpartdealslideorder,
  provideralldealslideorder,
  provideralldealslidehedgeorder
} from "@/api/order";

export const btnDeal = [
  {
    name: "全部成交",
    clickEvent: "allDealHedge",
    flag: "allDealHedge"
  },
  {
    name: "全部成交",
    clickEvent: "allDeal",
    flag: "allDeal"
  },
  {
    name: "部分成交",
    clickEvent: "partDeal",
    flag: "partDeal"
  }
];

export const deal = {
  data() {
    return {
      partDealVisible: false, // 部分成交弹窗
      partDealAmount: undefined, // 部分成交数
      partDealAmountLoading: false // 部分成交提交loading
    };
  },
  methods: {
    // 套保订单全部成交
    allDealHedge(data) {
      let _this = this;
      this.$confirm({
        title: "该订单已进行套保处理，是否确认手动操作订单全部成交",
        onOk() {
          return new Promise((resolve, reject) => {
            provideralldealslidehedgeorder({
              orderId: data.id,
              version: data.version
            })
              .then(res => {
                _this.$message.success("操作成功");
                _this.$emit("handleSuccess", {
                  data: res,
                  type: "allDealHedge"
                });
                resolve();
              })
              .catch(() => {
                reject();
              });
          });
        }
      });
    },
    // 卖家，点价全部成交
    allDeal(data) {
      let _this = this;
      this.$confirm({
        title: "此操作将成交剩余数量, 是否继续?",
        onOk() {
          return new Promise((resolve, reject) => {
            provideralldealslideorder({
              orderId: data.id,
              version: data.version
            })
              .then(res => {
                _this.$message.success("操作成功");
                _this.$emit("handleSuccess", { data: res, type: "allDeal" });
                resolve();
              })
              .catch(() => {
                reject();
              });
          });
        }
      });
    },
    // 卖家，点价部分成交
    partDeal(data) {
      this.partDealAmount = undefined;
      this.partDealVisible = true;
      this.handleRow = data;
    },
    // 卖家，提交部分成交
    submitPartDeal() {
      // if (
      //   this.partDealAmount &&
      //   this.partDealAmount <=
      //     parseInt(
      //       (
      //         Number(this.handleRow.amount) - Number(this.handleRow.dealAmount)
      //       ).toFixed(2)
      //     ) &&
      //   this.partDealAmount > 0
      // ) {
      this.partDealAmountLoading = true;
      providerpartdealslideorder({
        orderId: this.handleRow.id,
        version: this.handleRow.version,
        dealAmount: this.partDealAmount
      })
        .then(res => {
          this.partDealAmount = undefined;
          this.partDealVisible = false;
          this.$message.success("操作成功");
          this.$emit("handleSuccess", { data: res, type: "partDeal" });
        })
        .finally(() => {
          this.partDealAmountLoading = false;
        });
      // } else {
      //   this.$message.warning("请输入正确成交数");
      // }
    }
  }
};
